package top.yunmouren.atom.webui.tools;

import net.minecraftforge.fml.loading.FMLLoader;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import static top.yunmouren.jcef.tools.ClassPath.*;
import static top.yunmouren.jcef.tools.ClassPath.property;

public class DependencyInitialization {
    public DependencyInitialization() {
        if (!nativeLibraryInitial() & !jcefLibraryInitial()) {
            System.exit(0);
        }
    }


    public String GameDirectory = FMLLoader.getGamePath().toUri().toString();
    public String Atom_JcefUrl = (top.yunmouren.jcef.tools.ClassPath.class.getProtectionDomain().getCodeSource().getLocation().getFile()).substring(1).split("%")[0];

    public boolean jcefLibraryInitial() {
        try {
            if (isJar(Atom_JcefUrl)) {
                jarWin64LibCopyFile(Atom_JcefUrl);
                addLibraryDir_JKD17(property + "\\win64");
            } else {
                ZipWin64LibCopyFile(Atom_JcefUrl + "win64.zip");
                addLibraryDir_JKD17(property + "\\win64");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean isJar(String url) {
        String[] files = url.split("\\.");
        return files[files.length - 1].equals("jar");
    }

    public boolean nativeLibraryInitial() {
        if (!isJar(Atom_JcefUrl)) {
            return false;
        }
//        添加依赖库文件目录
        List<String> libsList = new ArrayList<>();
        libsList.add("gluegen_rt.dll");
        libsList.add("jogl_desktop.dll");
        libsList.add("jogl_mobile.dll");
        libsList.add("nativewindow_awt.dll");
        libsList.add("nativewindow_win32.dll");
        libsList.add("newt_head.dll");
//        创建依赖库文件目录
        try {
            new File(new URI(GameDirectory + "natives")).mkdir();
            new File(new URI(GameDirectory + "natives/windows-amd64")).mkdir();
        } catch (URISyntaxException e) {
            e.printStackTrace();
            return false;
        }
//        循环创建写入依赖库文件
        for (String lib : libsList) {
            try {
                URL copyLibUrl = new URL("jar:file:/" + Atom_JcefUrl + "!/natives/windows-amd64/" + lib);
                File copyToLibUrlCreate = new File(new URI(GameDirectory + "natives/windows-amd64/" + lib));
                copyToLibUrlCreate.createNewFile();
                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(copyToLibUrlCreate));
                System.out.println(copyLibUrl);
                out.write(copyLibUrl.openStream().readAllBytes());
                out.close();
            } catch (URISyntaxException | IOException e) {
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }
}
